package com.ck.javacode.Chapter6;

/**
 * @author: caoke
 * @date: 2021/10/6 4:48 下午
 * @description:
 */
public class D5_ArrayReverse {
    public static void main(String[] args) {
        //要求:把数组的元素内容反转。 ArrayReverse.java
        //arr {11,22,33,44,55,66} {66, 55,44,33,22,11}

        int[] arr1 = {11,22,33,44,55,66};
        int [] arr2 = new int[arr1.length];
        for (int i =0; i<arr1.length; i++){
            arr2[arr1.length-i-1]=arr1[i];
        }
        for (int i = 0; i<arr2.length; i++){
            System.out.println(arr2[i]);
        }

        //老韩思路
        //规律
        //1. 把 arr[0] 和 arr[5] 进行交换 {66,22,33,44,55,11}
        //2. 把 arr[1] 和 arr[4] 进行交换 {66,55,33,44,22,11}
        //3. 把 arr[2] 和 arr[3] 进行交换 {66,55,44,33,22,11}
        //4. 一共要交换 3 次 = arr.length / 2
        //5. 每次交换时，对应的下标 是 arr[i] 和 arr[arr.length - 1 -i] //代码
        //优化
        int temp = 0;
        int len = arr1.length;
        for (int i = 0; i < len/2; i++){
            temp = arr1[len-1-i];
            arr1[len-1-i] = arr1[i];
            arr1[i] = temp;
        }


        for (int i = 0; i<arr1.length; i++){
            System.out.println(arr1[i]);
        }

    }
}
